package aom.ju.ss.view.user.shop.management.freightTemplate

import android.content.Intent
import android.graphics.Color
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.GridLayoutManager
import aom.ju.ss.R
import aom.ju.ss.databinding.NewShippingTemplateActivityItemBinding
import aom.ju.ss.databinding.NewShippingTemplateBinding
import aom.ju.ss.lpInterface.BindViewInterface
import aom.ju.ss.utils.show
import aom.ju.ss.view.base.BaseActivity
import aom.ju.ss.view.base.PAdapter

/**
 * 新建运费模板
 */
class NewShippingTemplate : BaseActivity(), BindViewInterface {
    private lateinit var binding: NewShippingTemplateBinding
    private lateinit var pAdapter: PAdapter
    private var list: MutableList<ProvinceMode> = arrayListOf()

    companion object {

        var plis = arrayListOf(
            "上海市区",
            "云南省",
            "内蒙古自治区",
            "北京市",
            "台湾",
            "吉林省",
            "四川省",
            "天津市",
            "宁夏回族自治区",
            "安徽省",
            "山东省",
            "山西省",
            "广东省",
            "广西壮族自治区",
            "新疆维吾尔自治区",
            "江苏省",
            "江西省",
            "河北省",
            "河南省",
            "浙江省",
            "海南省",
            "湖北省",
            "湖南省",
            "澳门特别行政区",
            "甘肃省",
            "福建省",
            "西藏自治区",
            "贵州省",
            "辽宁省",
            "重庆市",
            "陕西省",
            "青海省",
            "香港特别行政区",
            "黑龙江省"
        )
    }

    private val backList = arrayListOf<String>()
    private var title = ""
    override fun init() {
        binding = viewBinding as NewShippingTemplateBinding
        title = intent.getStringExtra("title")!!
        binding.title.title.text = title
        binding.title.title.setTextColor(Color.WHITE)
        binding.title.r_title_two.text = "    保存    "
        binding.title.r_title_two.setTextColor(Color.WHITE)
        binding.title.root_view.setBackgroundColor(Color.parseColor("#8444F8"))
        pAdapter = PAdapter(list, R.layout.new_shipping_template_activity_item, this)
        binding.list.small.isEnabled = false
        binding.list.mRecycler.layoutManager = GridLayoutManager(activity, 2)

        initData()

        binding.list.mRecycler.adapter = pAdapter

        binding.title.r_title_two.setOnClickListener {//保存

            for (a in list) {
                if (a.type == 2) {
                    backList.add(a.name)
                }
            }
//            if (backList.size == 0) {
//                "请选择$title".show()
//                return@setOnClickListener
//            }

            val back = Intent().putStringArrayListExtra("list", backList)
            setResult(RESULT_OK, back)
            finish()

//            token:81547b2be4445228e2217421f1957f75c05bd76b
//            client_id:1
//            platform:android
//            shop_id:53 //店铺的id    必填
//            type:3  //模版类型   1特惠邮费 2常规邮费 3偏远地区邮费 4不配送区域
//            province:["西藏","新疆","内蒙古","黑龙江"]  //配送区域, json格式


//            OKHttpUtils(activity)
//                .SetApiUrl(LP_API.addShipping)
//                .SetKey("token", "client_id", "shop_id", "type", "province", "api_version","platform")
//                .SetValue(
//                    UserInfo.get().userInfo.data.token,
//                    "1",
//                    LpUserUtils.get().shopId,
//                    intent.getStringExtra("type"),
//                    Gson().toJson(backList),
//                    "v2",
//                    "android"
//                )
//                .POST(this)

        }
        binding.ckAll.setOnClickListener {
            for (a in list) {
                if (a.type != 0) {
                    a.type = if (binding.ckAll.isChecked) 2 else 1
                }
            }
            pAdapter.notifyDataSetChanged()
        }

    }

//    override fun onBackPressed() {
//
//        for (a in list) {
//            if (a.type == 2) {
//                backList.add(a.name)
//            }
//        }
//        if (backList.size != 0) {
//            val back = Intent().putStringArrayListExtra("list", backList)
//            setResult(RESULT_OK, back)
//        }
//
//        finish()
//    }

    private fun initData() {
        for (p in plis) {
            if (EditShippingTemplate.selectedProvince.contains(p)) {
                list.add(ProvinceMode(p, 0))
            } else {
                list.add(ProvinceMode(p, 1))
            }
        }
    }

    override val lay: Int =   R.layout.new_shipping_template

    override fun bandView(b: ViewDataBinding?, position: Int) {
        val ib = b as NewShippingTemplateActivityItemBinding
        ib.info = list[position]

        ib.ckv.setOnClickListener {
            when (list[position].type) {
                0 -> {
                    val msg = list[position].name + "已在其他区域中"
                    msg.show
                }
                1 -> {
                    list[position].type = 2
                    pAdapter.notifyItemChanged(position)
                }
                2 -> {
                    list[position].type = 1
                    pAdapter.notifyItemChanged(position)
                }
            }

        }


    }


}

class ProvinceMode(var name: String, var type: Int)// 0不可选择  1未选中 2已选中